#!/bin/bash
HOME="/home"
PHP_INSTALL_DIR="/usr/local"
PHP_INSTALL="php7410"
PHP_FLOD="php-7.4.10"
PHP_TAR="$PHP_FLOD.tar.gz"
PHP_URL="http://mirrors.sohu.com/php/$PHP_TAR"
# gd-devel要大于2.1
#创建用户及用户组方法
function createUserGroupAndUser() {
    group=$1
    user=$2
    egrep "^$group" /etc/group >&/dev/null
    #创建用户组
    if [[ $? -ne 0 ]]; then
        groupadd $group
    fi
    #创建用户组下面的用户
    egrep "^$user" /etc/passwd >&/dev/null
    if [ $? -ne 0 ]; then
        useradd -r -s /sbin/nologin -g $group $user
    fi
}
function installPHP() {
    #yum安装依赖的包
    rpm -q yum &>/dev/null
    if [ $? -ne 0 ]; then
        echo "请先安装yum工具"
        exit
    fi
    rpm -q wget &>/dev/null
    if [ $? -ne 0 ]; then
        yum install wget -y
    fi

    yum -y install gcc gcc-c++ make autoconf automake libxml2 libxml2-devel \
    openssl openssl-devel bzip2 \
    bzip2-devel libcurl libcurl-devel libjpeg libjpeg-devel libpng libpng-devel \
    freetype freetype-devel gmp gmp-devel \
    readline readline-devel libxslt libxslt-devel \
    zlib sqlite-devel

    if [ ! -d "$HOME" ]; then
        mkdir $HOME
    fi

    cd "$HOME"
    #下载镜像并解压Nginx源码包
    if [ ! -f "$HOME/$PHP_TAR" ]; then
        wget -c $PHP_URL && tar -zxf $PHP_TAR
    fi
    cd $PHP_FLOD
    #创建用户组
    createUserGroupAndUser www www
    #预编译  https://www.php.net/manual/zh/migration74.other-changes.php#migration74.other-changes.pkg-config
    ./configure --prefix=$PHP_INSTALL_DIR/$PHP_INSTALL \
    --with-config-file-path=$PHP_INSTALL_DIR/$PHP_INSTALL/etc \
    --with-fpm-user=www --with-fpm-group=www \
    --with-mhash --with-openssl --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd \
    --with-iconv --with-zlib --enable-inline-optimization \
    --disable-debug --disable-rpath \
    --enable-shared --enable-bcmath \
    --enable-shmop --enable-sysvsem --enable-sysvmsg --enable-sysvshm \
    --enable-gd --with-jpeg --with-freetype \
    --enable-mbregex --enable-mbstring \
    --enable-ftp --enable-pcntl \
    --enable-sockets --enable-soap \
    --without-pear --with-gettext \
    --enable-session --with-curl \
    --enable-opcache --enable-fpm --with-gmp --with-readline --enable-calendar --enable-exif \
    --without-gdbm --enable-fast-install \
    --enable-pcntl --enable-posix \
    --disable-fileinfo

    if [ $? -ne 0 ]; then
        echo "php预编译失败"
        exit
    else
        echo "php预编译成功"
    fi
    make clean && make -j 4 && make install
    if [ $? -ne 0 ]; then
        echo "make或make install失败"
        exit
    else
        echo "make成功"
    fi
    #软连接(直接通过php来进行操作)
    ln -s $PHP_INSTALL_DIR/$PHP_INSTALL/bin/php /usr/bin/php
    #配置文件的操作
    cp php.ini-production $PHP_INSTALL_DIR/$PHP_INSTALL/etc/php.ini #php的配置文件
    cp sapi/fpm/php-fpm.conf $PHP_INSTALL_DIR/$PHP_INSTALL/etc/php-fpm.conf
    cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm #php的自动启动
    cp $PHP_INSTALL_DIR/$PHP_INSTALL/etc/php-fpm.d/www.conf.default $PHP_INSTALL_DIR/$PHP_INSTALL/etc/php-fpm.d/www.conf
    chmod +x /etc/init.d/php-fpm
    chkconfig php-fpm on #添加到开机启动
}
installPHP
